-- @nsURI Mapping=http://www.eclipse.org/emf/2002/Mapping -- @nsURI XSD=http://www.eclipse.org/xsd/2002/XSD -- @nsURI Ecore=http://www.eclipse.org/emf/2002/Ecore -- @nsURI ATL=http://www.eclipse.org/gmt/2005/ATL -- Header section module HOT-extraction; create OUTmodel : ATL from INmodel : Mapping; -- Import section -- ******* -- HELPERS -- ******* -- ************* -- MATCHED RULES -- ************* --- ------------------------------------------------------ --- This rule takes a MappingRoot and creates --- an ATL module like this: --- --- -- @nsURI MM_Name=............. --- -- @nsURI XML=/http://www.eclipse.org/am3/2007/XML --- --- -- Header section --- module MM_Name_Ecore_To_XML; --- create OUTmodel : XML from INmodel : MM_Name; --- --- -- ***************************************** --- -- ********************** ATTRIBUTES helpers --- -- ***************************************** --- --- --- -- ***************************************** --- -- ********************** FUNCTIONAL helpers --- -- ***************************************** --- --- --- -- ***************************************** --- -- ************************* MATCHED RULES * --- -- ***************************************** --- --- rule MainContainer_EClass_Name { --- from --- input : MM_Name!MainContainer_EClass_Name --- to --- output1 : XML!Root ( --- ... --- ), --- ... --- do { --- --- } --- } --- --- -- Other matched rules --- --- ------------------------------------------------------ rule MappingRoot_to_Module { from input : Mapping!MappingRoot using { domainMM : Ecore!EPackage = input.inputs -> first(); } to output1 : ATL!Module ( -- attrs. commentsBefore <- '-- @nsURI ' + domainMM.name.toUpper() + '=' + domainMM.nsURI + '\n', commentsBefore <- '-- @nsURI XML=http://www.eclipse.org/am3/2007/XML' + '\n\n', commentsBefore <- '-- Header section', -- location <- , name <- domainMM.name.toUpper() + '_Ecore_to_XML', isRefining <- false, -- refs. inModels <- output2, outModels <- output3 -- libraries <- , -- elements <- {do section} ), output2 : ATL!OclModel ( -- input model -- attrs. -- location <- , name <- 'INmodel', -- refs. metamodel <- output4 -- elements <- ), output3 : ATL!OclModel ( -- output model -- attrs. -- location <- , name <- 'OUTmodel', -- refs. metamodel <- output5 -- elements <- ), output4 : ATL!OclModel ( -- source MM -- attrs. -- location <- , name <- domainMM.name.toUpper(), -- refs. -- metamodel <- Ecore, -- elements <- model <- output2 ), output5 : ATL!OclModel ( -- target MM -- attrs. -- location <- , name <- 'XML', -- refs. -- metamodel <- Ecore, -- elements <- model <- output3 ) do { for (mapping in input.nested) { thisModule.CreateMatchedRule(output1, mapping, output4, output5); } } } -- ************ -- CALLED RULES -- ************ --- -------------------------------------------------- --- This rule takes a Mapping on an Class and creates --- a matched rule like the this: --- --- rule EClass_Name_to_ComplexType_Name { --- from --- input : MM_Name!EClass_Name --- using { --- --- } --- to --- output : XML!Element ( --- name <- 'ComplexType_QualifiedName' --- ) --- do { --- --- } --- } --- -------------------------------------------------- rule CreateMatchedRule(modul : ATL!Module, mapping : Mappings!MappingForEClass, sourceMM : ATL!OclModel, targetMM : ATL!OclModel) { using { eClassName : String = mapping.inputs -> first().name; complexTypeName : String = mapping.outputs -> first().name; complexTypeQualifiedName : String = mapping.outputs -> first().qName; -- mappingsType1 : Sequence(Mappings!MappingType1) = mapping.mappings -> select(m | m.oclIsTypeOf(Mappings!MappingType1)); -- mappingsType2 : Sequence(Mappings!MappingType2) = mapping.mappings -> select(m | m.oclIsTypeOf(Mappings!MappingType2)); -- mappingsType3 : Sequence(Mappings!MappingType3) = mapping.mappings -> select(m | m.oclIsTypeOf(Mappings!MappingType3)); -- mappingsType4 : Sequence(Mappings!MappingType4) = mapping.mappings -> select(m | m.oclIsTypeOf(Mappings!MappingType4)); -- mappingsType5 : Sequence(Mappings!MappingType5) = mapping.mappings -> select(m | m.oclIsTypeOf(Mappings!MappingType5)); } to output1 : ATL!MatchedRule( -- attrs. -- location <- , name <- eClassName + '_to_' + complexTypeName, isAbstract <- false, isNoDefault <- false, isRefining <- false, -- refs. -- module <- inPattern <- output2, -- variables <- outPattern <- output5, actionBlock <- output10 ), -- ------------------------------- -- from -- input : MM_Name!EClass_Name -- ------------------------------- output2 : ATL!InPattern( -- attrs. -- location <- , -- refs. -- rule <- output1, elements <- output3 ), output3 : ATL!SimpleInPatternElement( -- input : -- attrs. -- location <- , varName <- 'input', -- refs. inPattern <- output2 ), output4 : ATL!OclModelElement ( -- : MM_Name!EClass_Name -- attrs. -- location <- , name <- eClassName, -- refs. model <- sourceMM, variableDeclaration <- output3 ), -- ------------------------------- -- to -- output : XML!Element (...), -- ------------------------------- output5 : ATL!OutPattern( -- attrs. -- location <- , -- refs. -- rule <- output1, elements <- output6 ), output6 : ATL!SimpleOutPatternElement( -- output : -- attrs. -- location <- , varName <- 'output', -- refs. outPattern <- output5, bindings <- output8 -- bindings <- {do section} ), output7 : ATL!OclModelElement ( -- : XML!Element -- attrs. -- location <- , name <- 'Element', -- refs. model <- targetMM, variableDeclaration <- output6 ), output8 : ATL!Binding( -- attrs. -- location <- , propertyName <- 'name', -- isAssignment <- -- refs. outPatternElement <- output6, value <- output9 ), output9 : ATL!StringExp( -- 'ComplexType_QualifiedName' -- attrs. -- location <- , stringSymbol <- complexTypeQualifiedName ), output10 : ATL!ActionBlock ( -- attrs. -- refs. -- statements <- {do section} ) do { -- -- bindings for the OutPatternElement (output6) -- for (mappingt5 in mappingsType5) { -- -- thisModule.CreateChunk1(output6, -- mappingt5.eRef.name, -- output3); -- } -- -- statements for the do-block (output10) -- for (mappingt1 in mappingsType1) { -- mappingt1.eAttr.upperBound = 1 -- -- thisModule.CreateChunk2(output10, -- mappingt1.xsdAttr.name, -- output3, -- mappingt1.eAttr.name, -- output6); -- } -- for (mappingt2 in mappingsType2) { -- -- } -- for (mappingt3 in mappingsType3) { -- -- if (mappingt3.eRef.upperBound = 1) { -- simple cardinality -- -- if (mappingt3.eRef.lowerBound = 1) -- required -- thisModule.CreateChunk4(output10, -- mappingt3.xsdAttr.name, -- output3, -- mappingt3.eRef.name, -- output6); -- else -- thisModule.CreateChunk5(output10, -- mappingt3.xsdAttr.name, -- output3, -- mappingt3.eRef.name, -- output6); -- } ---- if (mappingt3.eRef.upperBound = -1) { -- multiple cardinality ---- ---- if (mappingt3.eRef.lowerBound = 1) -- required ---- thisModule. ---- else ---- thisModule. ---- } ---- } -- } -- for (mappingt4 in mappingsType4) { -- mappingt1.eRef.upperBound = -1 ---- thisModule.CreateChunk(); -- } modul.elements <- output1; } } --- ----------------------------------------------------------------------------------------------- --- This rule is meant to be applied on a MappingType5 (corresponding to a containment EReference). --- --- It creates a binding for the EReference 'children' of an XML!Element: --- --- children <- input.ERef_Name --- --- ------------------------------------------------------------------------------------------------ rule CreateChunk1(outPatternElem : ATL!OutPatternElement, eRefName : String, inPatternElem : ATL!InPatternElement) { to output1 : ATL!Binding( -- attrs. -- location <- , propertyName <- 'children', -- isAssignment <- -- refs. outPatternElement <- outPatternElem, value <- output2 ), output2 : ATL!NavigationOrAttributeCallExp( -- input.ERef_Name -- attrs. -- location <- name <- eRefName, -- refs source <- output3 -- input. -- appliedProperty <- ), output3 : ATL!VariableExp( -- input. -- attrs. -- location <- -- refs referredVariable <- inPatternElem, appliedProperty <- output2 ) do { outPatternElem.bindings <- output1; } } --- ----------------------------------------------------------------------------------------------- --- This rule is meant to be applied on a MappingType1 corresponding to an EAttribute [1] or [0..1] (by the moment) --- --- It creates an invocation of the called rule "CreateXMLattr_StringValue" --- --- rule CreateXMLattr_StringValue(attrName : String, --- attrValue : String, --- attrParent : XML!Element); --- --- inside the do-block of a matched rule created from a MappingForEClass. --- --- Something like this: --- --- do { --- thisModule.CreateXMLattr_StringValue('Attr_Name', --- input.EAttribute_Name.toString(), --- output); --- } --- ----------------------------------------------------------------------------------------------- rule CreateChunk2(doBlock : ATL!ActionBlock, attrName : String, inPatternElem : ATL!InPatternElement, eAttrName : String, outPatternElem : ATL!OutPatternElement) { to output1 : ATL!ExpressionStat( -- attrs. -- location <- , -- refs. expression <- output2 ), output2 : ATL!OperationCallExp( -- attrs. -- location <- , operationName <- 'CreateXMLattr_StringValue', -- refs source <- output3, -- thisModule. -- appliedProperty <- arguments <- output5, arguments <- output6, arguments <- output9 ), output3 : ATL!VariableExp ( -- thisModule. -- attrs. -- location <- , -- refs. referredVariable <- output4, appliedProperty <- output2 ), output4 : ATL!VariableDeclaration( -- thisModule -- attrs. -- location <- , varName <- 'thisModule', -- refs. variableExp <- output3 ), output5 : ATL!StringExp( -- 'Attr_Name' -- attrs. -- location <- , stringSymbol <- attrName, -- refs. parentOperation <- output2 ), output6 : ATL!OperationCallExp( -- attrs. -- location <- , operationName <- 'toString', -- refs source <- output7, -- input.EAttribute_Name. -- appliedProperty <- -- arguments <- parentOperation <- output2 ), output7 : ATL!NavigationOrAttributeCallExp( -- input.EAttribute_Name. -- attrs. -- location <- name <- eAttrName, -- refs source <- output8, -- input. appliedProperty <- output6 ), output8 : ATL!VariableExp( -- input. -- attrs. -- location <- -- refs referredVariable <- inPatternElem, appliedProperty <- output7 ), output9 : ATL!VariableExp( -- output. -- attrs. -- location <- -- refs referredVariable <- outPatternElem, parentOperation <- output2 ) do { doBlock.statements <- output1; } } --- -------------------------------------------------------------------------------------- --- This rule is meant to --- -------------------------------------------------------------------------------------- --rule CreateChunk3() {} --- -------------------------------------------------------------------------------------- --- This rule is meant to be applied on a MappingType3 corresponding to an EReference [1]. --- --- It creates an invocation of the called rule "CreateXMLattr_StringValue" --- --- rule CreateXMLattr_StringValue(attrName : String, --- attrValue : String, --- attrParent : XML!Element); --- --- inside the do-block of a matched rule created from a MappingForEClass. --- --- Something like this: --- --- do { --- thisModule.CreateXMLattr_StringValue('Ref_Name', --- input.ERef_Name.name, --- output); --- } --- -------------------------------------------------------------------------------------- rule CreateChunk4(doBlock : ATL!ActionBlock, refName : String, inPatternElem : ATL!InPatternElement, eRefName : String, outPatternElem : ATL!OutPatternElement) { to output1 : ATL!ExpressionStat( -- attrs. -- location <- , -- refs. expression <- output2 ), output2 : ATL!OperationCallExp( -- attrs. -- location <- , operationName <- 'CreateXMLattr_StringValue', -- refs source <- output3, -- thisModule. -- appliedProperty <- arguments <- output5, arguments <- output6, arguments <- output9 ), output3 : ATL!VariableExp ( -- thisModule. -- attrs. -- location <- , -- refs. referredVariable <- output4, appliedProperty <- output2 ), output4 : ATL!VariableDeclaration( -- thisModule -- attrs. -- location <- , varName <- 'thisModule', -- refs. variableExp <- output3 ), output5 : ATL!StringExp( -- 'Ref_Name' -- attrs. -- location <- , stringSymbol <- refName, -- refs. parentOperation <- output2 ), output6 : ATL!NavigationOrAttributeCallExp( -- attrs. -- location <- , name <- 'name', -- refs source <- output7, -- input.ERef_Name. -- appliedProperty <- parentOperation <- output2 ), output7 : ATL!NavigationOrAttributeCallExp( -- input.ERef_Name. -- attrs. -- location <- name <- eRefName, -- refs source <- output8, -- input. appliedProperty <- output6 ), output8 : ATL!VariableExp( -- input. -- attrs. -- location <- -- refs referredVariable <- inPatternElem, appliedProperty <- output7 ), output9 : ATL!VariableExp( -- output. -- attrs. -- location <- -- refs referredVariable <- outPatternElem, parentOperation <- output2 ) do { doBlock.statements <- output1; } } --- ----------------------------------------------------------------------------------------- --- This rule is meant to be applied on a MappingType3 corresponding to an EReference [0..1]. --- --- It creates a CONDITIONAL invocation of the called rule "CreateXMLattr_StringValue" --- --- rule CreateXMLattr_StringValue(attrName : String, --- attrValue : String, --- attrParent : XML!Element); --- --- inside the do-block of a matched rule created from a MappingForEClass. --- --- Something like this: --- --- do { --- if (not input.ERef_Name.oclIsUndefined()) { --- thisModule.CreateXMLattr_StringValue('Ref_Name', --- input.ERef_Name.name, --- output); --- } --- } --- ----------------------------------------------------------------------------------------- rule CreateChunk5(doBlock : ATL!ActionBlock, refName : String, inPatternElem : ATL!InPatternElement, eRefName : String, outPatternElem : ATL!OutPatternElement) { to output1 : ATL!IfStat( -- attrs. -- location <- , -- refs. condition <- output2, thenStatements <- output6 ), -- ------------------------------------ -- not input.ERef_Name.oclIsUndefined() -- ------------------------------------ output2 : ATL!OperatorCallExp( -- attrs. -- location <- , operationName <- 'not' ), output3 : ATL!OperationCallExp( -- .oclIsUndefined() -- attrs. -- location <- , operationName <- 'oclIsUndefined', -- refs source <- output4, -- input.ERef_Name. appliedProperty <- output2 -- arguments <- ), output4 : ATL!NavigationOrAttributeCallExp( -- input.ERef_Name. -- attrs. -- location <- name <- eRefName, -- refs source <- output5, -- input. appliedProperty <- output3 ), output5 : ATL!VariableExp ( -- input. -- attrs. -- location <- , -- refs. referredVariable <- inPatternElem, appliedProperty <- output4 ), output6 : ATL!ExpressionStat( -- attrs. -- location <- , -- refs. expression <- output8 ), output8 : ATL!OperationCallExp( -- attrs. -- location <- , operationName <- 'CreateXMLattr_StringValue', -- refs source <- output9, -- thisModule. -- appliedProperty <- arguments <- output11, arguments <- output12, arguments <- output15 ), output9 : ATL!VariableExp ( -- thisModule. -- attrs. -- location <- , -- refs. referredVariable <- output10, appliedProperty <- output8 ), output10 : ATL!VariableDeclaration( -- thisModule -- attrs. -- location <- , varName <- 'thisModule', -- refs. variableExp <- output9 ), output11 : ATL!StringExp( -- 'Ref_Name' -- attrs. -- location <- , stringSymbol <- refName, -- refs. parentOperation <- output8 ), output12 : ATL!NavigationOrAttributeCallExp( -- attrs. -- location <- , name <- 'name', -- refs source <- output13, -- input.ERef_Name. -- appliedProperty <- parentOperation <- output8 ), output13 : ATL!NavigationOrAttributeCallExp( -- input.ERef_Name. -- attrs. -- location <- name <- eRefName, -- refs source <- output14, -- input. appliedProperty <- output12 ), output14 : ATL!VariableExp( -- input. -- attrs. -- location <- -- refs referredVariable <- inPatternElem, appliedProperty <- output13 ), output15 : ATL!VariableExp( -- output. -- attrs. -- location <- -- refs referredVariable <- outPatternElem, parentOperation <- output8 ) do { doBlock.statements <- output1; } } --- -------------------------------------------------------------------------------------- --- This rule is meant to --- -------------------------------------------------------------------------------------- --rule CreateChunk6() {} --- -------------------------------------------------------------------------------------------------- --- This rule is meant to be applied on a MappingType4 corresponding to an EReference [1..*] o [0..*]. --- --- It creates an invocation of the called rule "ContributeXMLelem_OfType_Model_Elem_Ref" --- --- rule ContributeXMLelem_OfType_Model_Elem_Ref (localName : String, --- modelElemName : String, --- parent : XML!Element) --- --- inside the do-block of a matched rule created from a MappingForEClass. --- --- Something like this: --- --- thisModule.ContributeXMLelem_OfType_Model_Elem_Ref('Elem_Name', --- modelElem.name, --- output); --- -------------------------------------------------------------------------------------------------- rule CreateChunk7(doBlock : ATL!ActionBlock, modelElemVarName : String, attrName : String, outPatternElem : ATL!OutPatternElement) { to output1 : ATL!ExpressionStat( -- attrs. -- location <- , -- refs. expression <- output2 ), output2 : ATL!OperationCallExp( -- attrs. -- location <- , operationName <- 'ContributeXMLelem_OfType_Model_Elem_Ref', -- refs source <- output3, -- thisModule. -- appliedProperty <- arguments <- output5, arguments <- output6, arguments <- output9 ), output3 : ATL!VariableExp ( -- thisModule. -- attrs. -- location <- , -- refs. referredVariable <- output4, appliedProperty <- output2 ), output4 : ATL!VariableDeclaration( -- thisModule -- attrs. -- location <- , varName <- 'thisModule', -- refs. variableExp <- output3 ), output5 : ATL!StringExp( -- 'Attr_Name' -- attrs. -- location <- , stringSymbol <- attrName, -- refs. parentOperation <- output2 ), output6 : ATL!NavigationOrAttributeCallExp( -- modelElem.name -- attrs. -- location <- name <- 'name', -- refs source <- output7 -- modelElem. -- appliedProperty <- ), output7 : ATL!VariableExp( -- modelElem. -- attrs. -- location <- -- refs referredVariable <- output8, appliedProperty <- output6 ), output8 : ATL!VariableDeclaration( -- -- attrs. -- location <- , varName <- modelElemVarName, -- refs. variableExp <- output7 ), output9 : ATL!VariableExp( -- output. -- attrs. -- location <- -- refs referredVariable <- outPatternElem, parentOperation <- output2 ) do { doBlock.statements <- output1; } }